import { ref, onMounted } from "vue"
import { useRoute } from 'vue-router'
import { getTopCategoryAPI } from '@/apis/category'
import { onBeforeRouteUpdate } from 'vue-router'
export function useCategory() {
    //分类数据
    const topCategory = ref([])
    const route = useRoute()
    const getTopCategory = async (id = route.params.id) => {
        const res = await getTopCategoryAPI(id)
        topCategory.value = res.data.result
    }
    // 组件挂载时获取数据
    onMounted(() => {
        getTopCategory()
    })

    // 路由更新前获取数据
    onBeforeRouteUpdate((to, from, next) => {
        getTopCategory(to.params.id) // 更新分类数据
        next() // 确保路由更新
    })

    return {
        topCategory
    }
}